package com.baijiayun.liveuibase.toolbox.answerer;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.baijiahulian.common.cropperv2.uikit.CircleColorView;
import com.baijiayun.livecore.models.LPUserAnswerDetail;
import com.baijiayun.livecore.utils.CommonUtils;
import com.baijiayun.liveuibase.R;
import com.baijiayun.liveuibase.base.BaseAdapter;
import com.baijiayun.liveuibase.base.ViewHolder;

import java.util.List;

public class RecordAdapter extends BaseAdapter<LPUserAnswerDetail> {
    private final String []colors = {
            "#FF607D",
            "#BE21E9",
            "#35CD3B",
            "#EE87FF",
            "#775FCF",
            "#AB8678",
            "#3D5AFE",
            "#03A9F4",
            "#C3FFA6",
            "#F44336",
            "#7ED321",
            "#FB9D3E",
            "#795548",
            "#FFEB3B",
            "#FFAE8E",
            "#E91E63"
    };
    public RecordAdapter(Context context, List<LPUserAnswerDetail> mDatas) {
        super(context, R.layout.uibase_item_answerer_record, mDatas);
    }

    @Override
    public void convert(ViewHolder helper, LPUserAnswerDetail item,int position) {
        TextView tvTitle = helper.getView(R.id.uibase_item_answerer_record_tv_title);
        TextView tvTime = helper.getView(R.id.uibase_item_answerer_record_tv_time);
        TextView tvSelected = helper.getView(R.id.uibase_item_answerer_record_tv_selected);
        final View view = helper.getView(R.id.rl_layout_group);
        TextView tvGroupName = helper.getView(R.id.tv_group_name);
        CircleColorView circleColorView = helper.getView(R.id.view_group_color);
        view.findViewById(R.id.view_space_active).setVisibility(View.GONE);
        if (item.user.groupItem != null && item.user.groupId != 0) {
            view.setVisibility(View.VISIBLE);
            tvGroupName.setText(item.user.groupItem.name);
            if (TextUtils.isEmpty(item.user.groupItem.color)) {
                int index = item.user.groupItem.id == 0 ? 0 : item.user.groupItem.id - 1;
                circleColorView.setColor(Color.parseColor(colors[index % 16]));
            } else {
                circleColorView.setColor(Color.parseColor(item.user.groupItem.color));
            }
        } else {
            view.setVisibility(View.GONE);
        }
        tvTitle.setText(CommonUtils.getEncodePhoneNumber(item.user.name));
        tvTime.setText(getTimeString(item.timeUsed));
        tvSelected.setText(getChoices(item.choices));
    }

    private String getTimeString(long seconds) {
        StringBuilder sb = new StringBuilder();
        long min = seconds / 60;
        long second = seconds % 60;
        if (seconds != 0) {
            sb.append(mContext.getString(R.string.base_answer_record_use_time));
            sb.append(min).append(min > 9 ? ":" : "0:")
                    .append(second > 9 ? "" : "0").append(second);
        }
        return sb.toString();
    }

    private String getChoices(String[] choices) {
        if (choices == null || choices.length == 0) {
            return mContext.getString(R.string.base_answer_record_not_choices);
        }
        StringBuilder sb = new StringBuilder();
        for (String s : choices) {
            sb.append(s).append(" ");
        }
        return sb.toString();
    }
}
